package com.geekhole.demo.v2.service;

import com.geekhole.demo.domain.ModifyUserNameContext;
import com.geekhole.demo.domain.User;
import com.geekhole.demo.domain.UserNotFoundException;
import com.geekhole.demo.v2.service.base.AbstractUpdateService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

@Service
public class ModifyUserNameService extends AbstractUpdateService<User, ModifyUserNameContext> {
    private final JpaRepository<User, Long> repository;
    public ModifyUserNameService(JpaRepository<User, Long> repository, ApplicationEventPublisher eventPublisher) {
        super(repository, eventPublisher);
        this.repository = repository;
    }

    @Override
    protected void update(User agg, ModifyUserNameContext context) {
        agg.modifyUserName(context.getNewName());
    }

    @Override
    protected void notFound(ModifyUserNameContext context) {
        throw new UserNotFoundException(context.getUserId());
    }

    @Override
    protected User loadAgg(ModifyUserNameContext context) {
        return this.repository.getById(context.getUserId());
    }
}
